<?php
// +----------------------------------------------------------------------
// | HkCms 菜单栏
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use common\controller\AdminBase;

class Menu extends AdminBase
{
    //菜单首页
    public function index() 
    {

        if ( request()->isPost() ) {

            $listorders = $_POST['listorders'];

            if (!empty($listorders)) {

                foreach ($listorders as $id => $v) {

                    model("admin/Menu")->find($id);

                    model("admin/Menu")->listorder = $v;

                    model("admin/Menu")->save();

                }

                $this->success('修改成功！', url('index'));

                exit; 
            }
        }

        $result = model("admin/Menu")->order(array("listorder" => "ASC"))->select()->toarray();

        $tree = new \libs\util\Tree();

        $tree->icon = array('&nbsp;&nbsp;&nbsp;│ ', '&nbsp;&nbsp;&nbsp;├─ ', '&nbsp;&nbsp;&nbsp;└─ ')
        ;
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';

        foreach ($result as $r) {

            $r['str_manage'] = '<a href="' . url("Menu/add", array("parentid" => $r['id'])) . '">添加子菜单</a> | <a href="' . url("Menu/edit", array("id" => $r['id'])) . '">修改</a> | <a class="J_ajax_del" href="' . url("Menu/delete", array("id" => $r['id'])) . '">删除</a> ';

            $r['status'] = $r['status'] ? "显示" : "不显示";

            $array[] = $r;
        }

        $tree->init($array);

        $str = "<tr>
	<td align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input'></td>
	<td align='center'>\$id</td>
	<td >\$spacer\$name</td>
                    <td align='center'>\$status</td>
	<td align='center'>\$str_manage</td>
	</tr>";

        $categorys = $tree->get_tree(0, $str);

        $this->assign("categorys", $categorys);

        return $this->fetch();
    }

    //添加菜单
    public function add() 
    {
        if ( request()->isPost() ) {

            if (model("admin/Menu")->data(input())->allowField(true)) {

                if (model("admin/Menu")->save()) {

                    $this->success("添加成功！", url("Menu/index"));
                } else {

                    $this->error("添加失败！");
                }
            } else {

                $this->error(model("admin/Menu")->getError());
            }
        }

        $tree = new \libs\util\Tree();

        $parentid = input('get.parentid', 0, 'intval');

        $result = model("admin/Menu")->select()->toarray();

        foreach ($result as $r) {

            $r['selected'] = $r['id'] == $parentid ? 'selected' : '';

            $array[] = $r;
        }

        $str = "<option value='\$id' \$selected>\$spacer \$name</option>";

        $tree->init($array);

        $select_categorys = $tree->get_tree(0, $str);

        $this->assign("select_categorys", $select_categorys);

        return $this->fetch();       
    }

    //编辑
    public function edit() 
    {
        if ( request()->isPost() ) {
            $post = input();
            $id = $post['id'];
            unset($post['id']);
            if (!empty($id) && !is_numeric($id)) {
                $this->error('id参数错误');
            }

            $result = $this->validate($post,'Menu');
            if(true !== $result){
                $this->error($result);
            }

            if (model("admin/Menu")->allowField(['parentid','name','app','controller','action','parameter','remark','status','type'])->save($post,['id'=>$id]) !== false) {

                $this->success("更新成功！", url("Menu/index"));

            } else {

                $this->error("更新失败！");
            }
        }

        $tree = new \libs\util\Tree();

        $id = input('id');

        $rs = model("admin/Menu")->find($id);

        $result = model("admin/Menu")->select()->toarray();

        foreach ($result as $r) {

            $r['selected'] = $r['id'] == $rs['parentid'] ? 'selected' : '';

            $array[] = $r;
        }

        $str = "<option value='\$id' \$selected>\$spacer \$name</option>";

        $tree->init($array);

        $select_categorys = $tree->get_tree(0, $str);

        $this->assign("data", $rs);

        $this->assign("id", $id);

        $this->assign("select_categorys", $select_categorys);

        return $this->fetch(); 
    }

    //删除
    public function delete() 
    {

        $id = input('id', 0, 'intval');

        $count = model("admin/Menu")->where(array("parentid" => $id))->count();

        if ($count > 0) {

            $this->error("该菜单下还有子菜单，无法删除！");
        }

        if (model("admin/Menu")->where([ 'id' => $id])->delete() !== false) {

            $this->success("删除菜单成功！");

        } else {

            $this->error("删除失败！");
        }
    }
}
